package com.example.qsindusty.jmm_spline.weight;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.qsindusty.jmm_spline.Config.Config;
import com.example.qsindusty.jmm_spline.R;
import com.example.qsindusty.jmm_spline.ReleaseOrder.CallBack2Lister;
import com.example.qsindusty.jmm_spline.ReleaseOrder.DialogConfig;
import com.example.qsindusty.jmm_spline.ReleaseOrder.GoodData;
import com.example.qsindusty.jmm_spline.ReleaseOrder.ReleasePresenter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;
import com.yixin.ystartlibrary.base.BaseRecyclerAdapter;
import com.yixin.ystartlibrary.utils.DensityUtil;
import com.yixin.ystartlibrary.utils.LogUtils;
import com.yixin.ystartlibrary.utils.Utils;
import com.yixin.ystartlibrary.widget.DiverItemDecoration;
import com.yixin.ystartlibrary.widget.EmptyRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administration on 2018/3/26.
 */

public class ChoiceGoodsDialog extends DialogFragment {
    DialogConfig mDialogConfig;
    OrderAdapter orderAdapter;
    ReleasePresenter mPresemnter;
    String row = Config.row;//默认为10条
    String page = "1"; //
    EmptyRecyclerView recyclerView;
    TextView goodstitle;
    ImageView ivEmpty;
    TextView emptyText;
    SmartRefreshLayout refreshLayout;
    LinearLayout dialogBackgounrd;
    List<GoodData.DataBean> dataBeanList ;
    List<GoodData.DataBean> dataBeans ;
    public ChoiceGoodsDialog() {
    }

    private static ChoiceGoodsDialog newIntance(DialogConfig config) {
        LogUtils.i("newIntance");
        ChoiceGoodsDialog choiceGoodsDialog = new ChoiceGoodsDialog();
        choiceGoodsDialog.initialize(config);
        return choiceGoodsDialog;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LogUtils.i("onCreate");
        Activity activity = getActivity();
        activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);


    }

    @Override
    public void onStart() {
        LogUtils.i("onStart");
        super.onStart();

    }

    private void initrefresh() {
        refreshLayout.setEnableScrollContentWhenLoaded(false);
        refreshLayout.setEnableAutoLoadmore(false);
        refreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                int p = Integer.parseInt(page);
              //  page = String.valueOf(p + 1);
                LogUtils.i("page==" + page);
              //  mPresemnter.getgoodsList(GeneralPreferencesUtils.getSharedPreference(getActivity(), "token", null), row, page);
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = "1";
                LogUtils.e("initrefreshpage==" + page);
             //   mPresemnter.getgoodsList(GeneralPreferencesUtils.getSharedPreference(getActivity(), "token", null), row, page);

            }
        });
    }

    @Override
    public void onResume() {
        LogUtils.i("onResume");
        super.onResume();
       int height = (int)(Utils.getHeight(getActivity())*0.6);
        Window window = getDialog().getWindow();
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, height);//Here!
        window.setGravity(Gravity.CENTER);
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        LogUtils.i("onCreateDialog");
        Dialog dialog = new Dialog(getActivity(), R.style.Dialog_NoTitle);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setContentView(initView());
        initrefresh();

        return dialog;
    }

    View initView() {
        LayoutInflater inflater = LayoutInflater.from(getContext());
        View view = inflater.inflate(R.layout.item_gooslist, null);
        LinearLayout linearLayout = view.findViewById(R.id.dialog_backgounrd);
        goodstitle=view.findViewById(R.id.goodstitle);
        refreshLayout =view.findViewById(R.id.refreshLayout);
        recyclerView=view.findViewById(R.id.recyclerview);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        emptyText=view.findViewById(R.id.empty_text);
        Button cancelbuttom =view.findViewById(R.id.cancel);
        Button surebuttom =view.findViewById(R.id.sure);
        cancelbuttom.setText(mDialogConfig.CANCEL);
        surebuttom.setText(mDialogConfig.SURE);
        DiverItemDecoration itemDecoration = new DiverItemDecoration(DiverItemDecoration.VERTICAL);
        itemDecoration.setSize(DensityUtil.dip2px(getActivity(), 0.5f));
        itemDecoration.setColor(ContextCompat.getColor(getActivity(), R.color.default_layout_color));

        recyclerView.addItemDecoration(itemDecoration);
        //解决ScrollView嵌套RecyclerView滑动卡帧
        recyclerView.setNestedScrollingEnabled(false);
        //色通知
        dataBeanList =new ArrayList<>();
        dataBeans =new ArrayList<>();
        orderAdapter = new OrderAdapter();
        recyclerView.setAdapter(orderAdapter);
        linearLayout.setBackgroundResource(mDialogConfig.BACKGOUNRDCOLOR);


        return view;
    }





    public static class Builder {
        DialogConfig mdialgConfig;


        public Builder() {
            mdialgConfig = new DialogConfig();
        }

        public Builder setBackgounrd(int colorres) {
            mdialgConfig.BACKGOUNRDCOLOR = colorres;
            return this;
        }

        public Builder setTitle(String title) {
            mdialgConfig.TITLE = title;
            return this;
        }

        public Builder setTitleSize(int titlesize) {
            mdialgConfig.TITLE_SIZE = titlesize;
            return this;
        }

        public Builder setTitleColor(int colorres) {
            mdialgConfig.TITLECORLOR = colorres;
            return this;
        }

        public Builder setCanceltext(String text) {
            mdialgConfig.CANCEL = text;
            return this;
        }

        public Builder setSuretext(String suretext) {
            mdialgConfig.SURE = suretext;
            return this;
        }

        public Builder setButttonColor(int colorres) {
            mdialgConfig.BUTTOMTEXTCOLOR = colorres;
            return this;
        }

        public Builder setcancelButttonColor(int colorres) {
            mdialgConfig.CANCELBUTTOMCOLOR = colorres;
            return this;
        }

        public Builder setsureButttonColor(int colorres) {
            mdialgConfig.SUREBUTTOMCOLOR = colorres;
            return this;
        }

        public Builder setCallback(CallBack2Lister callback) {
            mdialgConfig.mCallBack2 = callback;
            return this;
        }


        public ChoiceGoodsDialog build() {
            return newIntance(mdialgConfig);
        }


    }


    private void initialize(DialogConfig config) {
        mDialogConfig = config;

    }

    public class OrderAdapter extends BaseRecyclerAdapter<GoodData.DataBean, OrderAdapter.ViewHolder> {

        boolean[] booleans ;
        @Override
        protected void onBindViewHolder(final int position, final ViewHolder holder) {

            GoodData.DataBean goods = getItem(position);
            holder.setvauels(goods);
            holder.goods.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (holder.ischoiced.getVisibility()==View.GONE){
                        holder.ischoiced.setVisibility(View.VISIBLE);
                        booleans[position]=true;
                    }
                    else{
                        holder.ischoiced.setVisibility(View.GONE);
                        booleans[position]=false;
                    }

                }
            });
        }

        @Override
        protected ViewHolder getViewHolder(ViewGroup parent, int viewType) {
            booleans= new  boolean[getItemCount()];
            return new ViewHolder(inflate(R.layout.item_choice, parent));

        }

        @Override
        protected void onBindItemClickListener(View v, int position) {
            super.onBindItemClickListener(v, position);


        }

        class ViewHolder extends RecyclerView.ViewHolder {
            @BindView(R.id.goods)
            TextView goods;
            @BindView(R.id.ischoied)
            ImageView ischoiced;

            ViewHolder(View view) {
                super(view);
                ButterKnife.bind(this, view);
            }

            void setvauels(GoodData.DataBean goodsname) {
                ischoiced.setVisibility(View.GONE);
                for (int s=0;s<booleans.length;s++){
                    booleans[s]=false;
                }
                goods.setText(goodsname.getMC());

            }
        }
        public  boolean[] getBooleans(){
            return  booleans;
        }

    }


}